﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOSingTowerMall;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;

namespace BLSingTowerMall
{
    public class EventHallBookingProcess
    {
        // Class Variables

        bool valid = true;
        string errorMessage = String.Empty;

        private void setError(string msg)
        {
            errorMessage += msg + Environment.NewLine;
            valid = false;
        }

        

        public bool Valid
        {
            get { return valid; }
        }
        

        public string ErrorMessage
        {
            get { return errorMessage; }

        }

        public string Validate(DateTime startDate, DateTime endDate)
        {
            if (startDate.ToString() == string.Empty
               || endDate.ToString() == string.Empty
               )
            {
                string errorMessage = "alert('Event Start date or End date should not be Blank');";
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                return errorMessage;

            }
            else if ((startDate.DayOfYear - endDate.DayOfYear) > 5)
            {

                string errorMessage = "alert('Event Can not be booked for more than 5 days');";
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                return errorMessage;
            }
            else if (startDate.Date < DateTime.Today.Date
                      || endDate.Date < DateTime.Today.Date)
            {
                string errorMessage = "alert('Check if Event End date or Start date is before Todays Date');";
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                return errorMessage;

                //}  else if (bookingDetails.eventStartDate.Date < DateTime.Today.Date 
                //            || bookingDetails.eventEndDate.Date < DateTime.Today.Date ) {
                //    string script = "alert('Check if Event End date or Start date is before Todays Date');";
                //    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                //    return;

            }
            else if (endDate.Date < startDate.Date)
            {
                string errorMessage = "alert('Check if entered Event End date is before Event Start date');";
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                return errorMessage;
            } else {
                string successMessage = "Success";
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
                return successMessage;
            }

        }

        public bool bookEventHall(IEventHallBooking newHallBooking)
        {
            if(Validate(newHallBooking.EventStartDateTime, newHallBooking.EventEndDateTime) != "Success") 
            { 
                    setError("Event start date or End date not correct");
                    return false;
                   }
                    if (valid)
                    {
                        DBSingTowerMallEntities cntx = new DBSingTowerMallEntities();
                        valid = true;
                        errorMessage = String.Empty;

                        EventHallBooking evntHallBooking = new EventHallBooking();

                        int lastBookID = cntx.EventHallBookings.Max(x => x.BookingID);
                        //int blockID = (from s in cntx.EventHalls
                        //               where s.HallID == newHallBooking.HallID
                        //               select s.BlockID).FirstOrDefault();

                        //Nullable<Decimal> pricePerDay = ((from b in cntx.Blocks
                        //                                  where b.BlockID == blockID
                        //                                  select b.PricePerDay).FirstOrDefault());

                        evntHallBooking.BookingID = lastBookID + 1;
                        evntHallBooking.HallID = newHallBooking.HallID;

                        evntHallBooking.EventStartDateTime = newHallBooking.EventStartDateTime;
                        evntHallBooking.EventEndDateTime = newHallBooking.EventEndDateTime;
                        evntHallBooking.EventTitle = newHallBooking.EventTitle.Trim();
                        evntHallBooking.EventDescription = newHallBooking.EventDescription;

                        evntHallBooking.UserName = newHallBooking.UserName.Trim();
                        evntHallBooking.IdentityNo = newHallBooking.IdentityNo;

                        evntHallBooking.CompanyName = newHallBooking.CompanyName.Trim();
                        evntHallBooking.CompanyAddress = newHallBooking.CompanyAddress;

                        evntHallBooking.ApprovalDateTime = new DateTime();
                        evntHallBooking.BookingDateTime = new DateTime();
                        evntHallBooking.TotalPrice = newHallBooking.TotalPrice;//(evntHallBooking.EventEndDateTime.DayOfYear - evntHallBooking.EventStartDateTime.DayOfYear) * pricePerDay;


                        cntx.AddToEventHallBookings(evntHallBooking);
                        cntx.SaveChanges();
                        return true;
                    } else {
                        return false;
                    }

           
        }
    }
}
